<template>
    <div id="box">
        <form action="">
            <h2>登录</h2>
            <div class="input">
                <span>邮箱</span>
                <input type="text" name="" id="email" v-model="email">
            </div>
            <div class="input">
                <span>密码</span>
                <input type="password" name="" id="password" v-model="password">
            </div>
            <p :style="{ color: color ? 'green' : 'red' }">{{ message }}</p>
            <div class="button">
                <button type="button" @click="handleClick">注册</button>
                <button type="button" @click="handleClick('login')">登录</button>
            </div>
        </form>
    </div>
</template>

<script>
import axios from 'axios'
axios.defaults.withCredentials = true;

export default {
    data() {
        return {
            email: '',
            password: '',
            message: '',
            color: false
        }
    },
    methods: {
        handleClick(method) {
            if (this.email.trim().length <= 0 || this.password.trim().length <= 0) {
                this.color = false;
                this.message = '未输入邮箱或密码';
                return false
            }

            const obj = {
                email: this.email,
                password: this.password
            }

            if (method == 'login') {
                axios.post('http://localhost:3000/normal/login', obj).then(res => {
                    this.color = true;
                    this.message = res.data.data.message;
                    this.$store.commit('changeCurrentUser', res.data.data);
                    this.$router.push({ path: '/recommend' });
                }, err => {
                    this.color = false;
                    this.message = err.response.data.message;
                })
            } else {
                axios.post('http://localhost:3000/normal/register', obj).then(res => {
                    this.color = true;
                    this.message = res.data.message + '请登录';
                }, err => {
                    this.color = false;
                    this.message = err.response.data.message;
                })
            }
        }
    }
}
</script>

<style lang="scss" scoped>
    #box {
        display: flex;
        flex-flow: column nowrap;
        justify-content: center;
        align-items: center;
        min-height: calc(100vh - 240px);

        form {
            position: relative;
            display: flex;
            flex-flow: column nowrap;
            justify-content: center;
            align-items: center;
            width: 30%;
            height: 30vh;
            min-height: 287px;
            margin: 50px 0;
            border: 1px solid #ccc;
            border-radius: 15px;

            h2 {
                position: absolute;
                top: 25px;
                left: 50%;
                transform: translateX(-50%);
                cursor: default;
            }

            .input {
                display: flex;
                flex-flow: row nowrap;
                justify-content: flex-start;
                align-items: center;
                width: 80%;
                height: 50px;
                padding-left: 5px;
                margin-bottom: 10px;
                line-height: 50px;

                span {
                    flex: 0 0 auto;
                    font-size: 1.1rem;
                    margin-right: 5px;
                    cursor: default;
                }

                input {
                    flex: 1 1 auto;
                    height: 100%;
                    padding-left: 10px;
                    border: none;
                    border-bottom: 1px solid lightblue;
                    font-size: 1.2rem;
                }
            }

            p {
                width: 100%;
                height: 20px;
                color: red;
                text-align: center;
                line-height: 20px;
            }

            .button {
                display: flex;
                flex-flow: row nowrap;
                justify-content: space-evenly;
                align-items: center;
                position: absolute;
                bottom: 0;
                left: 0;
                width: 100%;
                height: 50px;
               
                button {
                    padding: 5px 10px;
                    background-color: rgba($color: lightblue, $alpha: 0.4);
                    border: none;
                    border-radius: 5px;
                    cursor: pointer;

                    &:hover {
                        background-color: lightblue;
                    }
                }
            }
        }
    }
</style>